package com.sky.controller.user;


import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Scope;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     * */
    @PostMapping
    @ApiOperation("新增地址")
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressBookService.saveAddress(addressBook);
        return Result.success();
    }

    //查询所有地址
    @GetMapping("/list")
    @ApiOperation("查询所有地址")
    public Result<List<AddressBook>> list(){
        log.info("查询所有地址");
        List<AddressBook> list = addressBookService.queryAllAddress();
        return Result.success(list);
    }

    //查询当条地址
    @GetMapping("/{id}")
    @ApiOperation("查询当条地址")
    public Result<AddressBook> queryById(@PathVariable Long id){
        log.info("查询当条地址");
        AddressBook addressBook = addressBookService.queryById(id);
        return Result.success(addressBook);
    }

    //根据id删除地址
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result delete(@RequestParam Long id){
        log.info("根据id删除地址");
        addressBookService.delete(id);
        return Result.success();
    }

    //设置默认地址
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址");
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    //查询默认地址
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }


    //修改地址
    @PutMapping
    @ApiOperation("修改地址")
    Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址：{}",addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }














}
